#coding=utf-8
from source.util.DateTimeUtil import *

class Base:
    def __init__(self,adb):
        self.adb=adb
        pass

    def current_activity(self):
        activity= self.adb.get_current_activity()
        return activity

    def current_package(self):
        package= self.adb.get_front_package_name()
        return package
        pass


    def send_key_event(self, test_action):
        '''
        通过native 发送键码值
        :param test_action:
        :return:
        '''
        # event_list={'home': 3, 'back': 4, 'menu': 82, 'ok': 23,
        #             'up': 19, "down": 20,"left":21,"right":22}
        # if isinstance(test_action,int):
        #     self.adb.keyevent(test_action)
        # else:
        #     if test_action in event_list.keys():
        #         self.adb.keyevent(event_list[test_action])
        #838C
        event_list = {'home': 102, 'back': 158, 'menu': 139, 'ok': 28,
                      'up': 103, "down": 108, "left": 105, "right": 106}

        #838 是/dev/input/event0,848是/dev/input/event1
        if isinstance(test_action, int):
            self.adb.keyevent_natvie(test_action,ir_device='/dev/input/event1')
        else:
            if test_action in event_list.keys():
                self.adb.keyevent_natvie(event_list[test_action],ir_device='/dev/input/event1')
        pass

    def tap(self,x,y,width,height):
        self.adb.tap(x, y, width, height)

    def swipe(self, st, sy, ex, ey):
        """
        滑动
        分别为:起始点x,y。结束点x,y。与滑动速度。滑动默认800
        """
        return self.adb.swipe(st, sy, ex, ey, duration=900)

    def get_window_size(self):
        """
        获取屏幕分辨率
        {u'width': 1080, u'height': 1920}
        :return: 1080,1920
        """

        screen_size = self.adb.get_window_size()
        width = screen_size['width']
        height = screen_size['height']
        return width, height

    def swipe_ratio(self, st, sy, ex, ey):
        """

        :param st: 起始点宽
        :param sy: 起始点高
        :param ex: 结束点宽
        :param ey: 结束点高
        :return: 滑动动作
        """
        width, height = self.get_window_size()
        return self.swipe(str(st * width), str(sy * height),
                          str(ex * width), str(ey * height))

    def swipe_left(self):
        """
        左滑屏幕
        """
        self.swipe_ratio(0.8, 0.5, 0.2, 0.5)
        sleep(1)

    def swipe_right(self):
        """
        右滑屏幕
        """
        self.swipe_ratio(0.2, 0.5, 0.8, 0.5)
        sleep(1)

    def swipe_up(self):
        """
        上滑屏幕
        """
        self.swipe_ratio(0.5, 0.8, 0.5, 0.2)
        sleep(1)

    def swipe_down(self):
        """
        下滑屏幕
        """
        self.swipe_ratio(0.5, 0.2, 0.5, 0.8)
        sleep(1)

    def swipe_all(self, t):
        """
        选择如何滑动屏幕
        """
        if t == 'swipe_left':
            self.swipe_left()
        elif t == 'swipe_right':
            self.swipe_right()
        elif t == 'swipe_up':
            self.swipe_up()
        elif t == 'swipe_down':
            self.swipe_down()

